package org.zzh.controller.cargo.contract;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;
import org.zzh.controller.BaseController;
import org.zzh.domain.cargo.contract.CoContract;
import org.zzh.domain.system.user.PeUser;
import org.zzh.service.cargo.contract.CoContractService;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/cargo/contract")
public class CoContractController extends BaseController {

    @Autowired
    public CoContractService coContractService;


    @RequestMapping(value = "/list.do")
    public ModelAndView list(
            @RequestParam(required = false, value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(required = false, value = "name", defaultValue = "") String name,
            @SessionAttribute("user") PeUser user
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageBean", coContractService.list(page, pageSize, name, user.getCompanyId()));
        modelAndView.addObject("name", name);
        modelAndView.setViewName("/WEB-INF/pages/cargo/contract/contract-list");
        return modelAndView;
    }

    @RequestMapping(value = "/toAdd.do")
    public ModelAndView toAdd() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/pages/cargo/contract/contract-add");
        return modelAndView;
    }

    @RequestMapping(value = "/toUpdate.do")
    public ModelAndView toUpdate(String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("contract", coContractService.findById(id));
        modelAndView.setViewName("/WEB-INF/pages/cargo/contract/contract-update");
        return modelAndView;
    }

    @RequestMapping(value = "/edit.do")
    public String edit(CoContract contract) {
        PeUser user = (PeUser) session.getAttribute("user");
        coContractService.edit(contract, user);
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping(value = "/delete.do")
    public String delete(String[] ids) {
        coContractService.deleteList(ids);
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping(value = "/toView.do")
    public ModelAndView toView(String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("contract", coContractService.findById(id));
        modelAndView.setViewName("/WEB-INF/pages/cargo/contract/contract-view");
        return modelAndView;
    }

    @RequestMapping(value = "/submit.do")
    public String submit(String id) {
        coContractService.submit(id);
        return "redirect:/cargo/contract/list.do";
    }


    @RequestMapping(value = "print.do")
    public ModelAndView print() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/pages/cargo/print/contract-print");
        return modelAndView;
    }

    @RequestMapping(value = "printExcel.do")
    public String printExcel(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date inputDate
    ) {
        return null;
    }

}
